from abc import ABC, abstractmethod

# 抽象组件
class Cipher(ABC):
    @abstractmethod
    def encrypt(self, plainText: str) -> str:
        pass

# 具体组件 - 简单加密（移位加密）
class SimpleCipher(Cipher):
    def encrypt(self, plainText: str) -> str:
        shift = 3  # 默认移位3
        result = []
        for char in plainText:
            if char.isalpha():
                if char.isupper():
                    result.append(chr((ord(char) - 65 + shift) % 26 + 65))
                else:
                    result.append(chr((ord(char) - 97 + shift) % 26 + 97))
            else:
                result.append(char)
        return ''.join(result)

# 抽象装饰器
class CipherDecorator(Cipher):
    def __init__(self, cipher: Cipher):
        self.cipher = cipher
    
    def encrypt(self, plainText: str) -> str:
        return self.cipher.encrypt(plainText)

# 具体装饰器 - 复杂加密（添加逆向功能）
class ComplexCipher(CipherDecorator):
    def encrypt(self, plainText: str) -> str:
        encrypted = self.cipher.encrypt(plainText)
        return self.reverse(encrypted)
    
    def reverse(self, text: str) -> str:
        return text[::-1]

# 具体装饰器 - 高级加密（添加求模功能）
class AdvancedCipher(CipherDecorator):
    def encrypt(self, plainText: str) -> str:
        encrypted = self.cipher.encrypt(plainText)
        return self.mod(encrypted)
    
    def mod(self, text: str, mod_value: int = 10) -> str:
        result = []
        for char in text:
            if char.isalpha():
                if char.isupper():
                    result.append(chr((ord(char) - 65) % mod_value + 65))
                else:
                    result.append(chr((ord(char) - 97) % mod_value + 97))
            else:
                result.append(char)
        return ''.join(result)

